package com.huawei.userservice.service.impl;


import com.huawei.common.pojo.Result;
import com.huawei.common.utils.JwtUtil;
import com.huawei.common.utils.Md5Util;
import com.huawei.common.utils.SnowflakeIdWorker;
import com.huawei.common.utils.ThreadLocalUtil;
import com.huawei.userservice.mapper.UserMapper;
import com.huawei.userservice.pojo.dto.UserDTO;
import com.huawei.userservice.pojo.entity.User;
import com.huawei.userservice.pojo.vo.UserVO;
import com.huawei.userservice.properties.SnowFlakeProperties;
import com.huawei.userservice.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SnowFlakeProperties snowFlakeProperties;

    @Override
    public User findByUsername(String username) {
        User u = userMapper.findByUsername(username);
        return u;
    }



    @Override
    public String makeJWT(Long id,String username) {
        Map<String,Object> claims =new HashMap<>();
        claims.put("id",id);
        claims.put("username",username);
        String token = JwtUtil.genToken(claims);
        return token;
    }

    @Override
    public Result renew(String token) {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ops.set(token,token,24, TimeUnit.HOURS);
        return new Result(0,"令牌成功延期一天",null);
    }

    @Override
    public Result logout(String token) {
        RedisOperations<String, String> operations = redisTemplate.opsForValue().getOperations();
        operations.delete(token);
        return Result.success("成功登出");
    }

    @Override
    public Result login(String username, String password) {
        //用户名是否被占用
        User loginUser = findByUsername(username);
        if(loginUser == null){
            return Result.error("用户名不存在");
        }
        if(Md5Util.getMD5String(password).equals(loginUser.getPassword())){
            String token = makeJWT(loginUser.getId(),username);
            ValueOperations<String, String> ops = redisTemplate.opsForValue();
            ops.set(token,token,1, TimeUnit.HOURS);
            return Result.success(token);
        }else{
            return Result.error("密码错误");
        }
    }

    @Override
    public Result register(String username, String password)    {

        //用户名是否被占用
        User user = findByUsername(username);
        if(user == null){
            SnowflakeIdWorker idWorker = new SnowflakeIdWorker(snowFlakeProperties.getWorkId(), snowFlakeProperties.getDataCenterId());
            long id = idWorker.nextId();
            //加密密码
            String md5String = Md5Util.getMD5String(password);
            userMapper.add(username,md5String,id);
            return Result.success();
        }else{
            return Result.error("用户名已占用");
        }
    }

    @Override
    public Result updateUserInfo(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        String[] userInfo = ThreadLocalUtil.get();
        user.setId(Long.parseLong(userInfo[0]));
        userMapper.updateUserInfo(user);
        return Result.success();
    }

    @Override
    public Result deleteUser(String token) {
        redisTemplate.delete(token);
        userMapper.deleteUser();
        return Result.success();
    }

    @Override
    public Result getUserInfo() {
        String[] userInfo = ThreadLocalUtil.get();
        User user = userMapper.findByUsername(userInfo[1]);
        System.out.println(user);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user,userVO);
        System.out.println(userVO);
        return new Result(0,"获取用户信息成功",userVO);
    }

    @Override
    public Result updateDyId(String douyueId) {
        User user = userMapper.findByDyId(douyueId);
        if(user == null){
            userMapper.updateDyId(douyueId);
            return new Result(0,"修改抖乐号成功！",null);
        }
        return Result.error("该抖乐号已绑定其他账号");

    }

    @Override
    public Result updateMoney(Long id,Integer money) {
        userMapper.updateMoney(money,id);
        return new Result(0,"金额修改成功",null);
    }

    @Override
    public Result pay(Long setterId, Integer money) {
        System.out.println("pay**********");
        userMapper.pay(setterId,money);
        return new Result(0,"支付成功",null);
    }
}
